import { defineStore } from "pinia";
import { ref } from "vue";
import {getLoginUserUsingGet} from "@/api/userController";
import ACCESS_ENUM from "@/access/accessEnum";

/**
 * 登录用户信息全局状态
 */
export const useLoginUserStore = defineStore("loginUser", () => {
    const loginUser = ref<API.LoginUserVO>({
        userName: "未登录"
    });

    function setLoginUser(newLoginUser: API.LoginUserVO) {
        // 将新的登录用户赋值给loginUser
        loginUser.value = newLoginUser;
    }

    async function fetchLoginUser() {
        const res = await getLoginUserUsingGet();
        if(res.data.code === 0 && res.data.data) {
            loginUser.value = res.data.data;
        }else {
            loginUser.value = { userRole: ACCESS_ENUM.NOT_LOGIN }
        }
    }

    function clearLoginUser () {
        loginUser.value = { userName: "未登录" };
    }

    return { loginUser, setLoginUser, fetchLoginUser,clearLoginUser };
});